package com.putao.tinytime.util;

import com.putao.tinytime.application.GlobalApplication;

import android.app.ActivityManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.LruCache;


public class BitmapHelper {
//    Canvas mCanvas;

    public BitmapHelper() {
//        mCanvas = new Canvas();
    }


    ActivityManager am = (ActivityManager) GlobalApplication.getInstance().getSystemService(GlobalApplication.getInstance().ACTIVITY_SERVICE);
    int memClassBytes = am.getMemoryClass() * 1024 * 1024;
    int cacheSize = memClassBytes / 4;
    LruCache<String, Bitmap> mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getByteCount();
        }
    };

    public Bitmap loadBitmapByThumbiId(String id) {
        Bitmap bitmap = getBitmapFromMemoryCache(id);
        if (bitmap == null || bitmap.isRecycled()) {
            bitmap = PhotoLoaderHelper.getThumbnailLocalBitmap(id);
            addBitmapToMemoryCache(id, bitmap);
        } else {
        }
        return bitmap;
    }

    public Bitmap loadBitmap(String url) {
        return loadBitmap(url, DisplayHelper.getScreenWidth(), DisplayHelper.getScreenHeight());
    }

    public Bitmap loadBitmap(String url, int width, int height) {
        Bitmap bitmap = getBitmapFromMemoryCache(url);
        if (bitmap == null || bitmap.isRecycled()) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(url, options);
            int sampleSize = calculateInSampleSize(options, width, height);
            options.inJustDecodeBounds = false;
            bitmap = PhotoLoaderHelper.getLocalBitmap(url, sampleSize);
            addBitmapToMemoryCache(url, bitmap);
        }
        return bitmap;
    }

    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        mMemoryCache.put(key, bitmap);
    }

    public Bitmap getBitmapFromMemoryCache(String key) {
        return mMemoryCache.get(key);
    }

    private static BitmapHelper instance;

    public static BitmapHelper getInstance() {
        if (instance == null) {
            instance = new BitmapHelper();
        }
        return instance;
    }

    //    // 从Resources中加载图片
    //    public static Bitmap decodeSampledBitmapFromResource(Resources res,
    //                                                         int resId, int reqWidth, int reqHeight) {
    //        final BitmapFactory.Options options = new BitmapFactory.Options();
    //        options.inJustDecodeBounds = true;
    //        BitmapFactory.decodeResource(res, resId, options); // 读取图片长款
    //        options.inSampleSize = calculateInSampleSize(options, reqWidth,
    //                reqHeight); // 计算inSampleSize
    //        options.inJustDecodeBounds = false;
    //        Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载入一个稍大的缩略图
    //        return createScaleBitmap(src, reqWidth, reqHeight); // 进一步得到目标大小的缩略图
    //    }
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            int heightRatio = (int) (height / (float) reqHeight);
            int widthRatio = (int) (width / (float) reqWidth);
            inSampleSize = (heightRatio < widthRatio) ? widthRatio : heightRatio;
        }
        return inSampleSize;
    }

    private static int calculateInSampleSizebak(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

    public Bitmap getBitmapFromPath(String path) {
        try {
            return BitmapFactory.decodeFile(path);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Bitmap getBitmapFromPathWithSize(String path, int targetwidth, int targetheight) {
        try {
            Bitmap result = BitmapFactory.decodeFile(path, null);
//            if (result.getWidth() > targetwidth || result.getHeight() > targetheight)
            {
                Matrix matrix = new Matrix();
                float ratio_current = result.getWidth() / (float) result.getHeight();
                float ratio_target = targetwidth / (float) targetheight;
                float scale = 1.0f;
                if (ratio_current < ratio_target) {//以高为准压缩
                    scale = (float) targetheight / result.getHeight();
                    matrix.postScale(scale, scale);
                    result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, false);
                } else {//以宽为准压缩
                    scale = (float) targetwidth / result.getWidth();
                    matrix.postScale(scale, scale);
                    result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, false);
                }
            }
            Loger.d("result size----->" + result.getWidth() + "," + result.getHeight());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public Bitmap getCenterCropBitmap(String path, int targetwidth, int targetheight) {
        try {
            Bitmap result = BitmapFactory.decodeFile(path, null);
//            if (result.getWidth() > targetwidth || result.getHeight() > targetheight) 
            {
                Matrix matrix = new Matrix();
                float ratio_current = result.getWidth() / (float) result.getHeight();
                float ratio_target = targetwidth / (float) targetheight;
                float scale = 1.0f;
                if (ratio_current >= ratio_target) {//以高为准压缩
                    scale = (float) targetheight / result.getHeight();
                    matrix.postScale(scale, scale);
                    result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, false);
                    result = Bitmap.createBitmap(result, (result.getWidth() - targetwidth) / 2, 0, targetwidth, targetheight);
                } else {//以宽为准压缩
                    scale = (float) targetwidth / result.getWidth();
                    matrix.postScale(scale, scale);
                    result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, false);
                    result = Bitmap.createBitmap(result, 0, (result.getHeight() - targetheight) / 2, targetwidth, targetheight);
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
